﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
using Microsoft.Practices.CompositeWeb.Interfaces;
using DataAccess.BusinessEntities;

namespace Locator.Stores.Views
{
    public class StoreSearchPresenter : Presenter<IStoreSearchView>, IDisposable 
    {
        private const string SearchResultsKey = "SearchResults";

        private readonly ISessionStateLocatorService sessionService;

        private readonly IStoresController controller;

        private List<Store> StoresSearchResult
        {
            get { return sessionService.GetSessionState()[SearchResultsKey] as List<Store>; }
            set { sessionService.GetSessionState()[SearchResultsKey] = value; }
        }

        public StoreSearchPresenter
            (
            [CreateNew] IStoresController controller, 
            [ServiceDependency] ISessionStateLocatorService sessionService
            )
        {
            this.controller = controller;
            this.sessionService = sessionService;
        }

        public override void OnViewLoaded()
        {
            this.View.SetSearchResults(this.StoresSearchResult);
            var cities = this.controller.AllCities();
            this.View.Cities = cities;
            this.LoadSearchRadiiOnView();
            this.LoadPersonalLocationsOnView();
        }

        private void LoadPersonalLocationsOnView()
        {
            var personalLocations = this.controller.GetPersonalLocationsForCurrentUser();
            if (personalLocations.Count > 0)
            {
                this.View.SetPersonalLocations(personalLocations);
                this.View.PersonalLocationSearchEnabled = true;
            }
            else
            {
                this.View.PersonalLocationSearchEnabled = false;
            }
        }

        public override void OnViewInitialized()
        {
            this.StoresSearchResult = null;
        }

        /// <summary>
        /// Loads the search radii on view.
        /// </summary>
        private void LoadSearchRadiiOnView()
        {
            this.View.SetSearchRadii(new List<int>() { 5, 10, 20, 3000 });
        }

        private void SetSearchResult(List<Store> stores)
        {
            this.StoresSearchResult = stores;
            this.View.SetSearchResults(this.StoresSearchResult);
        }

        public void OnSearchByCityClicked()
        {
            List<Store> stores = this.controller.FindStoresInCity(this.View.CityToSearch);
            this.SetSearchResult(stores);
        }

        public void OnStoreAddedForUser(Store storeInformation)
        {
            this.controller.AddStoreForCurrentUser(storeInformation.Id);
            // Update the added store to indicate that it's now used by the user
            this.StoresSearchResult.Find(store => store.Id == storeInformation.Id).UsedByUser = true;
            this.View.SetSearchResults(this.StoresSearchResult);
        }

        public void OnSearchByPostalCodeClicked()
        {
            var stores = this.controller.FindStoresByPostalCode(this.View.PostalCodeToSearch, this.View.PostalCodeSearchRadius);
            this.SetSearchResult(stores);
        }

        public void OnSearchByPersonalLocationClicked()
        {
            var stores = this.controller.FindStoresByPersonalLocation(this.View.PersonalLocationId, this.View.PersonalLocationSearchRadius);
            this.SetSearchResult(stores);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (controller != null)
                {
                    controller.Dispose();
                }
            }
        }

        ~StoreSearchPresenter()
        {
            Dispose(false);
        }
    }
}




